ConfigPath := A_ScriptDir . "\config.auto"
ENCODING := "UTF-8"
SPLIT := "@!"
NewLine := "`r`n"
LoadConfig()
{
    if !FileExist(ConfigPath)
    {
        ;文件不存在
        return
    }

    fp := FileOpen(ConfigPath,"r" , ENCODING)

    data := Map()

    while !fp.AtEOF
    {
        lineStrs :=  StrSplit(fp.ReadLine(),SPLIT)
        start := data
        
        endValue := lineStrs[lineStrs.Length]
        for index, value in lineStrs
        {
            ;倒数第二个
            if(index == lineStrs.Length - 1)
            {
                start[value] := endValue
                break
            }
            else
            {
                if !start.Has(value)
                {
                    start[value] := Map()
                }
                start := start[value]
            }
        }
    }
    fp.Close()
    return data
}


SaveConfig(config)
{
    valueStack := []
    valueStack.Push(config)
    keyStack := ["Config"]
    counts := [config.Count]
    args := []
    fp := FileOpen(ConfigPath, "w", ENCODING)
    while valueStack.Length > 0
    {
        data := valueStack.Pop()
        args.Push(keyStack.Pop())

        if (Type(data) == "Map")
        {
            for key, value in data
            {
                valueStack.Push(value)
                keyStack.Push(key)
            }
            counts.Push(data.Count)
        }
        else
        {
            for index, arg in args
            {
                fp.Write(arg . SPLIT)
            }
            fp.Write(data . NewLine)
            counts.Push(counts.Pop()-1)
            args.Pop()
        }
        cCount := counts.Pop()
        if(cCount==0)
        {
            args.Pop()
            counts.Push(counts.Pop()-1)
        }
        else
        {
            counts.Push(cCount)
        }
    }
    fp.Close()
}

PotionConfigName := "药剂配置"
PotionNames := ["药剂栏1","药剂栏2","药剂栏3","药剂栏4","药剂栏5"]
IntervalName := "间隔"
TimesName := "次数"
EnableName := "启用"

globalConfig := LoadConfig()

SaveConfig(globalConfig)

config := globalConfig["Config"][PotionConfigName]

MainGUI := Gui("-MaximizeBox", PotionConfigName)
Potion_LV := MainGUI.Add("ListView", "r6 w300", ["名    称",IntervalName . "(ms)", TimesName, EnableName])
Potion_LV.OnEvent("DoubleClick", Potion_DClick)


for index,name in PotionNames
{
    interval := config[name][IntervalName]
    times := config[name][TimesName]
    enable := config[name][EnableName]
    Potion_LV.Add(, name, )
    UpdateRowData(config[name], Potion_LV, index)
}


MainGUI.Show()

UpdateRowData(config, lv, index)
{
    interval := config[IntervalName]
    times := config[TimesName]
    enable := config[EnableName]
    lv.Modify(index, "Col2", interval,times<=0?"无限":times, enable > 0?"Yes":"No")
}

SettingGUI := Gui("+Owner -MaximizeBox -MinimizeBox", "设置")
SettingGUI.OnEvent("Close",SettingGUI_Closed)
SettingGUI.Add("Text","y+5 w150",IntervalName . "(ms)")
IntervalET := SettingGUI.Add("Edit", "w150")
SettingGUI.Add("Text","y+5 w150",TimesName)
TimesEdt := SettingGUI.Add("Edit", "w150",TimesName)
EnableCB := SettingGUI.Add("Checkbox", "y+10 w150",EnableName)
SaveBtn := SettingGUI.Add("Button","y+10 w150", "保存")
SaveBtn.OnEvent("Click", Save_Click)

; CreateSettingWindow(interval, times, enable, save_event, close_event)
; {
;     SettingGUI := Gui("+Owner -MaximizeBox -MinimizeBox", "设置")
;     SettingGUI.OnEvent("Close",close_event)
;     SettingGUI.Add("Text","y+5 w150",IntervalName . "(ms)")
;     IntervalET := SettingGUI.Add("Edit", "w150")
;     SettingGUI.Add("Text","y+5 w150",TimesName)
;     TimesEdt := SettingGUI.Add("Edit", "w150",TimesName)
;     EnableCB := SettingGUI.Add("Checkbox", "y+10 w150",EnableName)
;     SaveBtn := SettingGUI.Add("Button","y+10 w150", "保存")
;     SaveBtn.OnEvent("Click", save_event)
; }
selRowIndex := 0
Potion_DClick(lv, rowIndex)
{
    global selRowIndex
    selRowIndex := rowIndex
    chooseOptionName := lv.GetText(selRowIndex, 1)
    SettingGUI.Title := chooseOptionName
    IntervalET.Value := config[chooseOptionName][IntervalName]
    TimesEdt.Value := config[chooseOptionName][TimesName]
    EnableCB.Value := config[chooseOptionName][EnableName]
    MainGUI.Opt("+Disabled")
    SettingGUI.Show()
}

Save_Click(btn, index)
{
    global selRowIndex, config, globalConfig
    chooseOptionName := Potion_LV.GetText(selRowIndex, 1)
    config[chooseOptionName][IntervalName] := IntervalET.Value
    config[chooseOptionName][TimesName] := TimesEdt.Value
    config[chooseOptionName][EnableName] := EnableCB.Value
    UpdateRowData(config[PotionNames[selRowIndex]], Potion_LV, selRowIndex)
    SettingGUI.Hide()
    MainGUI.Opt("-Disabled")
    SaveConfig(globalConfig)
}

SettingGUI_Closed(guiObj)
{
    MainGUI.Opt("-Disabled")
}

F3 UP::Reload
F4 UP::ExitApp